package com.cn.bulu.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.cn.bulu.dao.CustomerDao;
import com.cn.bulu.dao.DealerDao;
import com.cn.bulu.pojo.Customer;
import com.cn.bulu.pojo.Customer_address;
import com.cn.bulu.pojo.Dealer;
import com.cn.bulu.pojo.Goods;
import com.cn.bulu.pojo.Indent;
import com.cn.bulu.pojo.Sample;
import com.cn.bulu.service.DealerService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.MD5Utils.MD5Utils;

import redis.clients.jedis.Jedis;
@Service("dealerService")
public class DealerServiceImpl implements DealerService {
	private Logger logger = Logger.getLogger(DealerServiceImpl.class);
	@Resource
	private DealerDao dealerDao;
	@Resource
	private CustomerDao customerDao;
	
	Jedis redis = new Jedis("localhost", 6379);// 连接redis
	//注册
	public BuluResult register(String phone, String password) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("phone",phone);
		map.put("password",password);
		int result= dealerDao.insertDealer(map);
		Map<String,Object> map2=new HashMap<String,Object>();
		if (result==1) {
			int dealerId=dealerDao.getDealerId(map);
			map2.put("dealerId",dealerId);
		}
		BuluResult buluResult=new BuluResult();	
		buluResult.setStatus(0);
		buluResult.setData(map2);
		return buluResult;
	}

	//判断是否可以注册
	public BuluResult isPhone(String phone) {
		int result=dealerDao.getDealerNumber(phone);
		BuluResult buluResult=new BuluResult();
		if (result==0) {
			buluResult.setStatus(0);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("该手机号已经被注册过了!");
		}
		return buluResult;
	}

	//完善信息
	public BuluResult completeDealerData(Integer dealerId, String name, String referrer, String storeName,
			String detailAddress, String imgs) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("dealerId", dealerId);
		map.put("name",name);
		map.put("referrer",referrer);
		map.put("storeName", storeName);
		map.put("detailAddress", detailAddress);
		dealerDao.updateDealerData(map);
		JSONArray jsonArray = JSONArray.parseArray(imgs);
		//把经销商的图片上传
		for(int i=0;i<jsonArray.size();i++){
			map.put("img",jsonArray.get(i));
			dealerDao.insertDealerImg(map);
		}
		BuluResult buluResult=new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	//登陆
	public BuluResult login(String phone, String password,HttpServletRequest request) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("phone",phone);
		map.put("password",password);
		Map<String, Object> result=dealerDao.selectDealer(map);
		BuluResult buluResult=new BuluResult();
		if (result==null) {
			//登录失败
			buluResult.setStatus(1);
			buluResult.setMessage("账号或者密码错误!!");
		}else{
			Integer dealerId=dealerDao.getDealerId(map);
			if(result.get("name")!=null){
				Integer dealerType=dealerDao.getDealerType(map);
				Integer status = dealerDao.selectStasusS(map);
				Map<String,Object> map2=new HashMap<String,Object>();
				map2.put("dealerId", dealerId);
				map2.put("status", status);
				map2.put("dealerType", dealerType);
				String sign = MD5Utils.getUUID();
				map2.put("sign", sign);
				redis.set(sign, String.valueOf(dealerId));
				buluResult.setData(map2);
				buluResult.setStatus(0);
			}else{
				Map<String, Object> send = new HashMap<>();
				send.put("dealerId", dealerId);
				buluResult.setData(send);
				buluResult.setStatus(2);
				buluResult.setMessage("未完善信息!!!");
			}
		}
		return buluResult;
	}

	//忘记密码
	public BuluResult getDealerPassword(String phone, String password) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("phone",phone);
		map.put("password",password);
		dealerDao.updateDealerPassword(map);
		BuluResult buluResult=new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}
	//退出
	
	//修改经销商的图片
	public BuluResult updateImgs(String[] imgs,HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		List<Integer> pictureIds=dealerDao.getPictureId((int)dealerId);
		if (pictureIds.size()>imgs.length) {
			for(int i=imgs.length-1;i<pictureIds.size();i++){
				dealerDao.deletePicture(pictureIds.get(i));
				pictureIds.remove(i);
			}
		}
		Map<String,Object> map=new HashMap<String,Object>();
		for(int i=0;i<imgs.length;i++){
			map.put("img", imgs[i]);
			map.put("pictureId", pictureIds.get(i));
			dealerDao.updateDealerImgs(map);
		}
		BuluResult buluResult=new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}
	//自营订单中的未完成
	public BuluResult NoComplete(HttpServletRequest request,Integer page) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		Integer type = dealerDao.selectType(dealerId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Integer> Mlist = dealerDao.selectReferrerP(phone);
		List<Integer> Clist = dealerDao.selectChildD(dealerId);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("Clist", Clist);
		List<Integer> CRlist = dealerDao.selectChildR(map2);
		Mlist.addAll(Clist);
		Mlist.addAll(CRlist);
		map.put("Mlist", Mlist);
		page=10*(page-1);
		map.put("page", page);
		List<Map<String, Object>> list = dealerDao.selectNOrder(map);
		BuluResult buluResult = new BuluResult();
		if(list.size()!=0&&list!=null){
			for(int a=0;a<list.size();a++){
				Integer zongshu = dealerDao.selectOrderGoods(list.get(a).get("orderId"));
				Integer yipinshu = dealerDao.selectOrderyiping(list.get(a).get("orderId"));
				if(zongshu==yipinshu){
					list.get(a).put("isEvaluation", 1);
				}else{
					list.get(a).put("isEvaluation", 0);
				}
				//加店员姓名
				Customer customer=dealerDao.getCustomerById(list.get(a).get("customerId"));
				list.get(a).put("NAME",customer.getNickName());
				list.get(a).put("samples", dealerDao.selectGoodsImg((Integer)list.get(a).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", list.get(a).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				list.get(a).put("orderProfit", sumGoods+sumGoodsPJ);
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无订单数据!!!");
		}
		return buluResult;
	}
	//查询商品利润详情 type是商品类型
	public BuluResult selectProfits(HttpServletRequest request,Integer type) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Map<String, Object> dealerProfits = dealerDao.selectProfits(dealerId);
		BuluResult buluResult = new BuluResult();
			List<Sample> list = dealerDao.selectGoodsProfit(type);
			for(int a=0;a<list.size();a++){
				//经销商是一级
				if((Integer)dealerProfits.get("type")==0){
					list.get(a).setFirstProfit(list.get(a).getFirstProfit()+(Double)dealerProfits.get("profits"));
				}else if((Integer)dealerProfits.get("type")==1){//经销商是二级
					list.get(a).setSecondProfit(list.get(a).getSecondProfit()+(Double)dealerProfits.get("profits"));
				}else if((Integer)dealerProfits.get("type")==2){//经销商是平台下自营
					list.get(a).setSecondProfit(
							list.get(a).getSecondProfit()+(Double)dealerProfits.get("profits")>=(Double)dealerProfits.get("minProfits")?list.get(a).getFirstProfit()+(Double)dealerProfits.get("profits"):(Double)dealerProfits.get("minProfits"));
				}
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
			return buluResult;
		
	}
	//查询子账号
	public BuluResult selectChild(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		List<Map<String, Object>> list = dealerDao.selectChild(dealerId);
		BuluResult buluResult = new BuluResult();
		if(list.size()!=0&&list!=null){
			buluResult.setStatus(0);
			buluResult.setData(list);
			return buluResult;
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无子账号!!!");
			return buluResult;
		}
	}
	//停用子账号
	public BuluResult chageChild(Integer customerId) {
		BuluResult buluResult = new BuluResult();
		dealerDao.chageChild(customerId);
		buluResult.setStatus(0);
		return buluResult;
	}
	//修改子账号密码
	public BuluResult chageChildPassword(Integer customerId, String password) {
		Map<String , Object> map = new HashMap<String ,Object>();
		map.put("customerId", customerId);
		map.put("password", password);
		dealerDao.chageChildPassword(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}
	//申请子账号
	public BuluResult applyChild(HttpServletRequest request, String nickName, String password) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("nickName", nickName);
		map.put("password", password);
		map.put("dealerId",request.getSession().getAttribute("dealerId"));
		dealerDao.applyChild(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}
	//查询可提现金额
	public BuluResult selectWallet(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Dealer dealer = dealerDao.selectWallet(dealerId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(dealer);
		return buluResult;
	}

	//查询一级经销商推荐客户数
	public BuluResult selectCustomerN(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		Integer type = dealerDao.selectType(dealerId);
		//TODO  这什么鬼
		Integer year = 2016;
		Map<String, Object> map = new HashMap<String, Object>();
		BuluResult buluResult = new BuluResult();
		if(type==0){
			//本身推荐的客户数
			Integer own = dealerDao.selectCustomerN(dealerId);
			Integer child = dealerDao.selectChildN(dealerId);
			Integer customerN = own+child;
			map.put("customerN", customerN);
			Integer SecondDealerN = dealerDao.selectSecond(dealerId);
			map.put("SecondDealerN", SecondDealerN);
			Map<String, Object> erji = new HashMap<>();
			erji.put("phone", phone);
			erji.put("year", year);
			List<Map<String, Object>> list = dealerDao.selectErmessage(erji);
			for(int a=0;a<list.size();a++){
				List<Integer> sumCustomer = new ArrayList<Integer>();
				//查询出当前年份的营业额
				List<Integer> Mlist = dealerDao.selectReferrerP((String)list.get(a).get("phone"));
				List<Integer> Clist = dealerDao.selectChildD((Integer)list.get(a).get("dealerId"));
				Map<String, Object> map2 = new HashMap<String,Object>();
				map2.put("Clist", Clist);
				List<Integer> CRlist = dealerDao.selectChildR(map2);
				Mlist.addAll(Clist);
				Mlist.addAll(CRlist);
				sumCustomer.addAll(Mlist);
				Map<String, Object> aaa = new HashMap<String,Object>();
				aaa.put("sumCustomer", sumCustomer);
				aaa.put("year", year);
				Double yearYingye = dealerDao.selectSumPrice(aaa);
				list.get(a).put("yearYingye", yearYingye);
				list.get(a).put("tuijian", sumCustomer.size());
			}
			map.put("jingxiaoshang", list);
			buluResult.setStatus(0);
			buluResult.setData(map);
			return buluResult;
		}else{
			Integer own = dealerDao.selectCustomerN(dealerId);
			Integer child = dealerDao.selectChildN(dealerId);
			Integer customerN = own+child;
			map.put("customerN", customerN);
			Map<String, Object> map2 = dealerDao.selectTask(dealerId);
			map.put("tasks", map2);
			buluResult.setStatus(0);
			buluResult.setData(map);
			return buluResult;
		}
	}
	//自营订单中的已完成
	public BuluResult YesComplete(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		Integer type = dealerDao.selectType(dealerId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Integer> Mlist = dealerDao.selectReferrerP(phone);
		List<Integer> Clist = dealerDao.selectChildD(dealerId);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("Clist", Clist);
		List<Integer> CRlist = dealerDao.selectChildR(map2);
		Mlist.addAll(Clist);
		Mlist.addAll(CRlist);
		map.put("Mlist", Mlist);
		List<Map<String, Object>> list = dealerDao.YesComplete(map);
		BuluResult buluResult = new BuluResult();
		if(list.size()!=0&&list!=null){
			for(int a=0;a<list.size();a++){
				//加店员姓名
				Customer customer=dealerDao.getCustomerById(list.get(a).get("customerId"));
				list.get(a).put("NAME",customer.getNickName());				
				list.get(a).put("samples", dealerDao.selectGoodsImg((Integer)list.get(a).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", list.get(a).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				list.get(a).put("orderProfit", sumGoods+sumGoodsPJ);
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无订单数据!!!");
		}
		return buluResult;
	}
	//查询二级未完成
	public BuluResult NoComplete2(HttpServletRequest request,Integer page) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		List<Map<String, Object>> scend = dealerDao.NoComplete2(phone);
		List<Map<String, Object>> sumlist = new ArrayList<Map<String,Object>>();
		for(int a=0;a<scend.size();a++){
			Map<String, Object> map = new HashMap<String,Object>();
			List<Integer> Mlist = dealerDao.selectReferrerP((String)scend.get(a).get("phone"));
			Integer type = (Integer)scend.get(a).get("type");
			List<Integer> Clist = dealerDao.selectChildD((Integer)scend.get(a).get("dealerId"));
			Map<String, Object> map2 = new HashMap<String,Object>();
			map2.put("Clist", Clist);
			List<Integer> CRlist = dealerDao.selectChildR(map2);
			Mlist.addAll(Clist);
			Mlist.addAll(CRlist);
			map.put("Mlist", Mlist);
			page=10*(page-1);
			map.put("page", page);
			List<Map<String, Object>> list = dealerDao.selectNOrder(map);
			if(list.size()!=0&&list!=null){
				for(int b=0;b<list.size();b++){
					Integer zongshu = dealerDao.selectOrderGoods(list.get(a).get("orderId"));
					Integer yipinshu = dealerDao.selectOrderyiping(list.get(a).get("orderId"));
					if(zongshu==yipinshu){
						list.get(a).put("isEvaluation", 1);
					}else{
						list.get(a).put("isEvaluation", 0);
					}
					list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("type", type);
					lirun.put("orderId", list.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectYiduier1(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					list.get(a).put("orderProfit", sumGoods+sumGoodsPJ);
				}
				sumlist.addAll(list);
			}
		}
		BuluResult buluResult = new BuluResult();
		if(sumlist!=null&&sumlist.size()>0){
			buluResult.setStatus(0);
			buluResult.setData(sumlist);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无订单数据!!!");
		}
		return buluResult;
	}

	//查询二级已完成订单
	public BuluResult YesComplete2(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		List<Map<String, Object>> scend = dealerDao.NoComplete2(phone);
		List<Map<String, Object>> sumlist = new ArrayList<Map<String,Object>>();
		for(int a=0;a<scend.size();a++){
			Map<String, Object> map = new HashMap<String,Object>();
			List<Integer> Mlist = dealerDao.selectReferrerP((String)scend.get(a).get("phone"));
			Integer type = (Integer)scend.get(a).get("type");
			List<Integer> Clist = dealerDao.selectChildD((Integer)scend.get(a).get("dealerId"));
			Map<String, Object> map2 = new HashMap<String,Object>();
			map2.put("Clist", Clist);
			List<Integer> CRlist = dealerDao.selectChildR(map2);
			Mlist.addAll(Clist);
			Mlist.addAll(CRlist);
			map.put("Mlist", Mlist);
			List<Map<String, Object>> list = dealerDao.YesComplete(map);
			if(list.size()!=0&&list!=null){
				for(int b=0;b<list.size();b++){
					list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("type", type);
					lirun.put("orderId", list.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectYiduier1(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					list.get(a).put("orderProfit", sumGoods+sumGoodsPJ);
				}
				sumlist.addAll(list);
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(sumlist);
		return buluResult;
	}
	//经销商自主下单测量订单
	public BuluResult insertCIndent(HttpServletRequest request,String name, String phone, Date subscribeTime, Integer roomNumB, String city,
			String address,String remark) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Customer_address customer_address = new Customer_address();
		customer_address.setName(name);
		customer_address.setPhone(phone);      
		customer_address.setCity(city);
		customer_address.setAddress(address);
		dealerDao.insertAddress(customer_address);
		Map<String, Object> map = new HashMap<String,Object>();
		int a = city.indexOf("省");
		String shi = city.substring(a+1);
		Map<String, Object> diyu = dealerDao.selectTerritory(shi);
		Double indentPrice = 0.00;
		if(roomNumB>5){
			indentPrice = (roomNumB-5)*(Double)diyu.get("dmeasureM")+(Double)diyu.get("measureM");
		}else{
			indentPrice = (Double)diyu.get("measureM");
		}
		Date beginTime = new Date();
		map.put("indentPrice", indentPrice);
		map.put("beginTime", beginTime);
		map.put("subscribeTime", subscribeTime);
		map.put("type", 0);
		map.put("dealerId", dealerId);
		map.put("remark", remark);
		map.put("cuAdId", customer_address.getCuAdId());
		map.put("roomNumB", roomNumB);
		int result = dealerDao.insertCIndent(map);
		BuluResult buluResult = new BuluResult();
		if(result!=1){
			buluResult.setStatus(1);
			buluResult.setMessage("因未知原因,添加失败!!!");
		}else{
			buluResult.setStatus(0);
		}
		return buluResult;
	}
	//点击提现记录
	public BuluResult selectRecord(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		List<Map<String, Object>> list = dealerDao.selectRecord(dealerId);
		BuluResult buluResult = new BuluResult();
		if(list!=null&&list.size()>0){
			buluResult.setStatus(0);
			buluResult.setData(list);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂无提现记录!!!");
		}
		return buluResult;
	}

	//经销商自主下单安装订单
	public BuluResult insertAIndent(HttpServletRequest request,String name, String phone, Date subscribeTime, String baseAddress,String detailAddress, String city,
			String address, String anyGoods) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Customer_address customer_address = new Customer_address();
		customer_address.setName(name);
		customer_address.setPhone(phone);
		customer_address.setCity(city);
		customer_address.setAddress(address);
		dealerDao.insertAddress(customer_address);
		int i = city.indexOf("省");
		String shi = city.substring(i+1, city.length()-1);
		logger.error(shi);
		//TODO 安装费待定  判断字符串中是否含有粘带
		Double indentPrice = 200.00;
		Date beginTime = new Date();
		Indent indent = new Indent();
		indent.setIndentPrice(indentPrice);
		indent.setBeginTime(beginTime);
		indent.setSubscribeTime(subscribeTime);
		indent.setCuAdId(customer_address.getCuAdId());
		indent.setBaseAddress(baseAddress);
		indent.setDetailAddress(detailAddress);
		indent.setType(1);
		indent.setDealerId(dealerId);
		dealerDao.insertAIndent(indent);
		JSONArray goodsArray = JSONArray.parseArray(anyGoods);
		if(goodsArray!=null&&goodsArray.size()>0){
			for(int a=0;a<goodsArray.size();a++){
				Goods goods = (Goods)goodsArray.get(a);
				dealerDao.insertGoods(goods);
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("goodsId", goods.getGoodsId());
				map.put("indentId", indent.getIndentId());
				dealerDao.insertGoods_Indent(map);
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	//盈利详情
	public BuluResult selectObtain(HttpServletRequest request, Integer year) {
		//总客户ID
		List<Integer> sumCustomer = new ArrayList<Integer>();
		//查询出当前年份的营业额和利润
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		Integer type = dealerDao.selectType(dealerId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Integer> Mlist = dealerDao.selectReferrerP(phone);
		List<Integer> Clist = dealerDao.selectChildD(dealerId);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("Clist", Clist);
		List<Integer> CRlist = dealerDao.selectChildR(map2);
		Mlist.addAll(Clist);
		Mlist.addAll(CRlist);
		sumCustomer.addAll(Mlist);
		List<Map<String, Object>> scend = dealerDao.NoComplete2(phone);
		//TODO 
		Double yearProfits1 = 0.0;
		List<Integer> Mlist2 = new ArrayList<>();
		for(int a=0;a<scend.size();a++){
			Mlist2 = dealerDao.selectReferrerP((String)scend.get(a).get("phone"));
			Integer type1 = (Integer)scend.get(a).get("type");
			List<Integer> Clist2 = dealerDao.selectChildD((Integer)scend.get(a).get("dealerId"));
			Map<String, Object> maper2 = new HashMap<String,Object>();
			maper2.put("Clist", Clist2);
			List<Integer> CRlist2 = dealerDao.selectChildR(maper2);
			Mlist2.addAll(Clist2);
			Mlist2.addAll(CRlist2);
			List<Map<String, Object>> list = dealerDao.YesComplete2(map);
			if(list.size()!=0&&list!=null){
				for(int b=0;b<list.size();b++){
					list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("type", type1);
					lirun.put("orderId", list.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectYiduier1(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					list.get(b).put("orderProfit", sumGoods+sumGoodsPJ);
				}
			}
			if(list!=null&&list.size()>0){
				for(int x=0;x<list.size();x++){
					yearProfits1+=(Double)list.get(x).get("orderProfit");
				}
			}
		}
		map.put("sumCustomer", sumCustomer);
		map.put("year", year);
		List<Map<String, Object>> list = dealerDao.YesComplete2(map);
		if(list.size()!=0&&list!=null){
			for(int b=0;b<list.size();b++){
				list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", list.get(b).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				list.get(b).put("orderProfit", sumGoods+sumGoodsPJ);
			}
		}
		Double yearProfits = 0.0;
		if(list!=null&&list.size()>0){
			for(int x=0;x<list.size();x++){
				yearProfits+=(Double)list.get(x).get("orderProfit");
			}
		}
		Map<String, Object> send = new HashMap<String,Object>();
		//加入到要发送的map里
		send.put("yearProfits", yearProfits+yearProfits1);
		sumCustomer.addAll(Mlist2);
		List<Map<String, Object>> histogramList = new ArrayList<Map<String,Object>>();
		Map<String, Object> mapTest = new HashMap<String,Object>();
		mapTest.put("year", year);
		mapTest.put("sumCustomer", sumCustomer);
		//改成5个年份的柱状图
		//查询上上一年的柱状图
		mapTest.put("dealerId", dealerId);
		Map<String, Object> mapOneOne = new HashMap<String,Object>();
		mapTest.put("year", year-2);
		mapOneOne.put("YEAR", year-2);
		if(dealerDao.selectTargetMoney(mapTest)==null){
			mapOneOne.put("targetMoney", 0);
		}else{
			mapOneOne.put("targetMoney", dealerDao.selectTargetMoney(mapTest));
		}
		logger.error(mapTest);
		if(dealerDao.selectSumPrice(mapTest)==null){
			mapOneOne.put("completeMoney", 0);
		}else{
			mapOneOne.put("completeMoney", dealerDao.selectSumPrice(mapTest));
		}
		histogramList.add(mapOneOne);
		//查询上一年份的柱状图
		mapTest.put("dealerId", dealerId);
		Map<String, Object> mapOne = new HashMap<String,Object>();
		mapTest.put("year", year-1);
		mapOne.put("YEAR", year-1);
		if(dealerDao.selectTargetMoney(mapTest)==null){
			mapOne.put("targetMoney", 0);
		}else{
			mapOne.put("targetMoney", dealerDao.selectTargetMoney(mapTest));
		}
		logger.error(mapTest);
		if(dealerDao.selectSumPrice(mapTest)==null){
			mapOne.put("completeMoney", 0);
		}else{
			mapOne.put("completeMoney", dealerDao.selectSumPrice(mapTest));
		}
		histogramList.add(mapOne);
		//查询当前年份的柱状图
		mapTest.put("dealerId", dealerId);
		Map<String, Object> mapTwo = new HashMap<String,Object>();
		mapTest.put("year", year);
		mapTwo.put("YEAR", year);
		if(dealerDao.selectTargetMoney(mapTest)==null){
			mapTwo.put("targetMoney", 0);
		}else{
			mapTwo.put("targetMoney", dealerDao.selectTargetMoney(mapTest));
		}
		if(dealerDao.selectSumPrice(mapTest)==null){
			mapTwo.put("completeMoney", 0);
		}else{
			mapTwo.put("completeMoney", dealerDao.selectSumPrice(mapTest));
		}
		histogramList.add(mapTwo);
		//查询下一年份的柱状图
		mapTest.put("dealerId", dealerId);
		Map<String, Object> mapThree = new HashMap<String,Object>();
		mapTest.put("year", year+1);
		mapThree.put("YEAR", year+1);
		if(dealerDao.selectTargetMoney(mapTest)==null){
			mapThree.put("targetMoney", 0);
		}else{
			mapThree.put("targetMoney", dealerDao.selectTargetMoney(mapTest));
		}
		if(dealerDao.selectSumPrice(mapTest)==null){
			mapThree.put("completeMoney", 0);
		}else{
			mapThree.put("completeMoney", dealerDao.selectSumPrice(mapTest));
		}
		histogramList.add(mapThree);
		//查询下下一年的柱状图
		mapTest.put("dealerId", dealerId);
		Map<String, Object> mapFour = new HashMap<String,Object>();
		mapTest.put("year", year+2);
		mapFour.put("YEAR", year+2);
		if(dealerDao.selectTargetMoney(mapTest)==null){
			mapFour.put("targetMoney", 0);
		}else{
			mapFour.put("targetMoney", dealerDao.selectTargetMoney(mapTest));
		}
		if(dealerDao.selectSumPrice(mapTest)==null){
			mapFour.put("completeMoney", 0);
		}else{
			mapFour.put("completeMoney", dealerDao.selectSumPrice(mapTest));
		}
		histogramList.add(mapFour);
		send.put("histogramList", histogramList);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(send);
		return buluResult;
	}
	//盈利详情中点击年利润出现自营
	public BuluResult selectProprietary(HttpServletRequest request, Integer year, Integer month) {
		//查询出当前年份的营业额和利润
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		logger.error(dealerId);
		String phone = dealerDao.selectPhone(dealerId);
		Integer type = dealerDao.selectType(dealerId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Integer> sumCustomer = dealerDao.selectReferrerP(phone);
		List<Integer> Clist = dealerDao.selectChildD(dealerId);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("Clist", Clist);
		List<Integer> CRlist = dealerDao.selectChildR(map2);
		sumCustomer.addAll(Clist);
		sumCustomer.addAll(CRlist);
		sumCustomer.add(-1);
		map.put("sumCustomer", sumCustomer);
		map.put("year", year);
		map.put("dealerId", dealerId);
		List<Map<String, Object>> list = dealerDao.YesComplete2(map);
		if(list.size()!=0&&list!=null){
			for(int b=0;b<list.size();b++){
				list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", list.get(b).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				logger.error(sumGoodsPJ);
				list.get(b).put("orderProfit", sumGoods+sumGoodsPJ);
			}
		}
		Double yearProfits = 0.0;
		if(list!=null&&list.size()>0){
			for(int x=0;x<list.size();x++){
				yearProfits+=(Double)list.get(x).get("orderProfit");
			}
		}
		Map<String, Object> send = new HashMap<String,Object>();
		send.put("yearProfits", yearProfits);
		map.put("yearMonth", year+""+"-"+""+month);
		List<Map<String, Object>> list2 = dealerDao.YesComplete3(map);
		if(list2.size()!=0&&list2!=null){
			for(int b=0;b<list2.size();b++){
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", list.get(b).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				list2.get(b).put("orderProfit", sumGoods+sumGoodsPJ);
			}
		}
		//月利润
		Double monthProfits = 0.0;
		if(list2!=null&&list2.size()>0){
			for(int x=0;x<list2.size();x++){
				monthProfits+=(Double)list2.get(x).get("orderProfit");
			}
		}
		send.put("monthProfits", monthProfits);
		List<Map<String, Object>> child = new ArrayList<Map<String,Object>>();
		for(int x=0;x<Clist.size();x++){
			Map<String, Object> childMap = new HashMap<String,Object>();
			childMap.put("nickName", dealerDao.selectName(Clist.get(x)));
			childMap.put("customerId", Clist.get(x));
			//查询单个子账号推荐的顾客ID
			List<Integer> singleList = dealerDao.selectSingleChild(Clist.get(x));
			singleList.add(Clist.get(x));
			logger.error(Clist);
			singleList.add(-1);
			Map<String, Object> chMap = new HashMap<String ,Object>();
			chMap.put("yearMonth", year+""+"-"+""+month);
			chMap.put("sumCustomer", singleList);
			List<Map<String, Object>> childList = dealerDao.YesComplete3(chMap);
			double childYingye = 0.0;
			double childLirun = 0.0;
			if(childList.size()!=0&&childList!=null){
				for(int b=0;b<childList.size();b++){
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("type", type);
					lirun.put("orderId", (Integer)childList.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					childLirun+=sumGoods+sumGoodsPJ;
					childYingye+=(Double)childList.get(b).get("customerPrice");
				}
			}
			childMap.put("childYingye", childYingye);
			childMap.put("childLirun", childLirun);
			childMap.put("nubmer", childList.size());
			child.add(childMap);
		}
		send.put("Child",child);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(send);
		return buluResult;
	}

	//(一级才用)盈利详情中点击年利润(二级经销商)
	public BuluResult selectScendProprietary(HttpServletRequest request, Integer year, Integer month) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		String phone = dealerDao.selectPhone(dealerId);
		List<Map<String, Object>> scend = dealerDao.NoComplete2(phone);
		Map<String, Object> send = new HashMap<>();
		Double yearProfits = 0.0;
		Double monthProfits = 0.0;
		List<Map<String,Object>> scendList = new ArrayList<>();
		List<Integer> sumCustomer = new ArrayList<Integer>();
		for(int a=0;a<scend.size();a++){
			Map<String, Object> map = new HashMap<String,Object>();
			List<Integer> Mlist = dealerDao.selectReferrerP(phone);
			List<Integer> Clist = dealerDao.selectChildD(dealerId);
			Map<String, Object> map2 = new HashMap<String,Object>();
			map2.put("Clist", Clist);
			List<Integer> CRlist = dealerDao.selectChildR(map2);
			Mlist.addAll(Clist);
			Mlist.addAll(CRlist);
			sumCustomer.addAll(Mlist);
			map.put("sumCustomer", sumCustomer);
			map.put("year", year);
			List<Map<String, Object>> list = dealerDao.YesComplete2(map);
			if(list.size()!=0&&list!=null){
				for(int b=0;b<list.size();b++){
					list.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list.get(b).get("orderId")));
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("orderId", list.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectYiduier1(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					yearProfits+=sumGoods+sumGoodsPJ;
				}
			}
			map.put("yearMonth", year+""+"-"+""+month);
			logger.error(year+""+"-"+""+month);
			List<Map<String, Object>> list2 = dealerDao.YesComplete3(map);
			Double monthProfits2 = 0.0;
			Double turnover = 0.0;
			Integer quantity  = 0;
			if(list2.size()!=0&&list2!=null){
				for(int b=0;b<list2.size();b++){
					Map<String, Object> lirun = new HashMap<>();
					lirun.put("dealerId", dealerId);
					lirun.put("orderId", list2.get(b).get("orderId"));
					Double sumGoods = dealerDao.selectYiduier1(lirun);
					if(sumGoods==null){
						sumGoods=0.0;
					}
					Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
					if(sumGoodsPJ==null){
						sumGoodsPJ=0.0;
					}
					turnover += (Double)list2.get(b).get("customerPrice");
					monthProfits2 += sumGoods+sumGoodsPJ;
				}
				quantity = list2.size();
			}
			Map<String, Object> erji = new HashMap<>();
			erji.put("dealerId", scend.get(a).get("dealerId"));
			erji.put("storeName", scend.get(a).get("storeName"));
			erji.put("monthProfits", monthProfits2);
			erji.put("turnover", turnover);
			erji.put("quantity", quantity);
			scendList.add(erji);
		}
		send.put("yearProfits", yearProfits);
		send.put("monthProfits", monthProfits);
		send.put("scendDealer", scendList);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(send);
		return buluResult;
	}

	//添加银行卡和提现密码和身份证图片
	public BuluResult updateXinka(HttpServletRequest request, String boundBank, String bankNumber,
			String recordPassword, String imgs) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Map<String, Object> map = new HashMap<>();
		map.put("boundBank", boundBank);
		map.put("bankNumber", bankNumber);
		map.put("recordPassword", recordPassword);
		map.put("dealerId", dealerId);
		JSONArray tupian = JSONArray.parseArray(imgs);
		if(tupian!=null&&tupian.size()>0){
			for(int a=0;a<tupian.size();a++){
				Map<String, Object> imgMap = new HashMap<>();
				imgMap.put("img", tupian.get(a));
				imgMap.put("dealerId", dealerId);
				
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	//查询订单中内容
	public BuluResult getGoodsByOrderId(HttpServletRequest request, Integer orderId) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Integer type = dealerDao.selectType(dealerId);
		logger.error(orderId);
		List<Map<String,Object>> list=customerDao.getSampleDetailByOrderId(orderId);
		for(int a=0;a<list.size();a++){
			Map<String, Object> map = new HashMap<>();
			Object goodsId = list.get(a).get("goodsId");
			map.put("dealerId", dealerId);
			map.put("type", type);
			map.put("goodsId", goodsId);
			Double lirun1 = dealerDao.selectOneGoods(map);
			if(lirun1==null){
				lirun1=0.0;
			}
			List<Map<String, Object>> acces = customerDao.getGoods_AccByGoodsId((int)goodsId);
			Double lirun2 = 0.0;
			if(acces!=null&&acces.size()>0){
				for(int b=0;b<acces.size();b++){
					Map<String, Object> map2 = new HashMap<>();
					map2.put("dealerId", dealerId);
					map2.put("type", type);
					map2.put("goodsId", acces.get(b).get("goodsId"));
					lirun2 += dealerDao.selectPeiProfitL(map2);
				}
			}
			
			//查询优惠活动
			list.get(a).put("goodsProfit", lirun1+lirun2);                                                                      
			list.get(a).put("acces",acces);
		}	
		Map<String, Object> map = new HashMap<>();
		//查询测量费
		Double celiang = dealerDao.selectCeliang(orderId);
		if(celiang==null){
			celiang = 0.0;
		}
		//map.put("celiang",celiang);
		//查询安装费
		Double anzhuang = dealerDao.selectAnzhuang(orderId);
		if(anzhuang==null){
			anzhuang = 0.0;
		}
		//map.put("anzhuang",anzhuang);
		Map<String, Object> send = dealerDao.selectMian(orderId);
		if(send.get("mian")!=null&&(Integer)send.get("mian")!=1){
			//map.put("mian",0);
		}else{
			//map.put("mian",celiang+anzhuang);
		}
		Double youhuiedu=0.0;
		if(send.get("couponsId")!=null){
			logger.error(send.get("couponsId"));
			youhuiedu = Double.parseDouble(((String)send.get("couponsId")).substring(((String) send.get("couponsId")).indexOf("减")+1,((String)send.get("couponsId")).indexOf("元")));
			//map.put("youhui",youhui);
		}
		//声明优惠分担和安装测量费分担
		Double youhuifen = dealerDao.selectyouhuifen(type);
		Double youhuifentan = youhuiedu*youhuifen;
		Double ancefen = dealerDao.selectceliangfen(type);
		//计算特价商品共多少钱
		Double tejia = dealerDao.selectTejia(orderId);
		Double zhengjia = dealerDao.selectzhengjia(orderId);
		Double youhui = dealerDao.selectyouhui(orderId);
		map.put("youhui", youhui);
		map.put("zhengjia", zhengjia);
		map.put("tejia", tejia);
		map.put("youhuifentan", youhuifentan);
		map.put("celiangfen", celiang*ancefen);
		map.put("anzhuangfen", anzhuang*ancefen);
		//把满减放里面
		//查询订单中的信息
		String youhuiBody = dealerDao.selectyouhuibody(orderId);
		map.put("youhuiBody", youhuiBody);
		String celiangBody = dealerDao.selectceanbody(orderId);
		map.put("celiangBody", celiangBody);
		map.put("anzhuangBody", celiangBody);
		//TODO   实际利润等于原始利润减去分摊的优惠钱数和安装测量分摊的钱数
		map.put("shiji", 2000);
		Map<String, Object> fasong = new HashMap<>();
		fasong.put("goods", list);
		fasong.put("qita", map);
		BuluResult buluResult=new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(fasong);
		return buluResult;
	}

	//盈利详情中点击子账号查询已完成订单
	public BuluResult selectChildOrder(HttpServletRequest request,Integer customerId,Integer year,Integer month) {
		logger.error(customerId+"------------"+year+"---------"+month);
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Integer type = dealerDao.selectType(dealerId);
		Map<String, Object> childMap = new HashMap<String,Object>();
		childMap.put("nickName", dealerDao.selectName(customerId));
		//查询单个子账号推荐的顾客ID
		List<Integer> singleList = dealerDao.selectSingleChild(customerId);
		singleList.add(customerId);
		singleList.add(-1);
		Map<String, Object> chMap = new HashMap<String ,Object>();
		chMap.put("yearMonth", year+""+"-"+""+month);
		chMap.put("sumCustomer", singleList);
		List<Map<String, Object>> childList = dealerDao.YesComplete3(chMap);
		BuluResult buluResult = new BuluResult();
		if(childList.size()!=0&&childList!=null){
			for(int b=0;b<childList.size();b++){
				childList.get(b).put("samples", dealerDao.selectGoodsImg((Integer)childList.get(b).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("type", type);
				lirun.put("orderId", (Integer)childList.get(b).get("orderId"));
				Double sumGoods = dealerDao.selectGoodsProfitL(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectGoodsPeiProfitL(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				childList.get(b).put("orderLirun", sumGoods+sumGoodsPJ);
				childList.get(b).put("nickName", dealerDao.selectName(customerId));
			}
			buluResult.setStatus(0);
			buluResult.setData(childList);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("暂时没有数据!!!");
		}
		return buluResult;
	}

	//根据订单号查询其中的商品一级对二级的利润
	public BuluResult selectScendGoods(HttpServletRequest request, Integer orderId) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Integer type = dealerDao.selectType(dealerId);
		logger.error(orderId);
		List<Map<String,Object>> list=customerDao.getSampleDetailByOrderId(orderId);
		for(int a=0;a<list.size();a++){
			Map<String, Object> map = new HashMap<>();
			Object goodsId = list.get(a).get("goodsId");
			map.put("dealerId", dealerId);
			map.put("type", type);
			map.put("goodsId", goodsId);
			Double lirun1 = dealerDao.selectGoodsYiduier1(map);
			List<Map<String, Object>> acces = customerDao.getGoods_AccByGoodsId((int)goodsId);
			Double lirun2 = 0.0;
			if(acces!=null&&acces.size()>0){
				for(int b=0;b<acces.size();b++){
					Map<String, Object> map2 = new HashMap<>();
					map.put("dealerId", dealerId);
					map.put("type", type);
					map.put("goodsId", acces.get(b).get("goodsId"));
					lirun2 += dealerDao.selectGoodsYiduier2(map2);
				}
			}
			list.get(a).put("goodsProfit", lirun1+lirun2);
			list.get(a).put("acces",acces);
		}	
		BuluResult buluResult=new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

	//(一级才用)根据经销商订单号查询其某年某月的订单
	public BuluResult selectScendDealer(HttpServletRequest request, Integer scendDealerId,Integer year,Integer month) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		List<Integer> sumCustomer = new ArrayList<Integer>();
		String phone = dealerDao.selectPhone(scendDealerId);
		Map<String, Object> map = new HashMap<String,Object>();
		List<Integer> Mlist = dealerDao.selectReferrerP(phone);
		List<Integer> Clist = dealerDao.selectChildD(scendDealerId);
		Map<String, Object> map2 = new HashMap<String,Object>();
		map2.put("Clist", Clist);
		List<Integer> CRlist = dealerDao.selectChildR(map2);
		Mlist.addAll(Clist);
		Mlist.addAll(CRlist);
		sumCustomer.addAll(Mlist);
		map.put("sumCustomer", sumCustomer);
		map.put("yearMonth", year+""+"-"+""+month);
		List<Map<String, Object>> list2 = dealerDao.YesComplete3(map);
		BuluResult buluResult = new BuluResult();
		if(list2.size()!=0&&list2!=null){
			for(int b=0;b<list2.size();b++){
				list2.get(b).put("samples", dealerDao.selectGoodsImg((Integer)list2.get(b).get("orderId")));
				Map<String, Object> lirun = new HashMap<>();
				lirun.put("dealerId", dealerId);
				lirun.put("orderId", list2.get(b).get("orderId"));
				Double sumGoods = dealerDao.selectYiduier1(lirun);
				if(sumGoods==null){
					sumGoods=0.0;
				}
				Double sumGoodsPJ = dealerDao.selectYiduier2(lirun);
				if(sumGoodsPJ==null){
					sumGoodsPJ=0.0;
				}
				list2.get(b).put("orderProfit", sumGoods+sumGoodsPJ);
			}
			buluResult.setStatus(0);
			buluResult.setData(list2);
		}else{
			buluResult.setStatus(1);
			buluResult.setMessage("本月没有订单完成!!!");
		}
		return buluResult;
	}

	//查询经销商个人信息
	public BuluResult selectDealer(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Map<String, Object> map = dealerDao.selectDealer2(dealerId);
		List<String> list = dealerDao.selectDealerImg(dealerId);
		map.put("imgs", list);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	//一级经销商查询自身二级的商品利润详情
	public BuluResult selectProfits2(HttpServletRequest request, Integer type) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Map<String, Object> dealerProfits = dealerDao.selectProfits(dealerId);
		BuluResult buluResult = new BuluResult();
			List<Sample> list = dealerDao.selectGoodsProfit(type);
			for(int a=0;a<list.size();a++){
				list.get(a).setFSProfit(list.get(a).getFSProfit()+(Double)dealerProfits.get("profits"));
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
			return buluResult;
	}

	//根据商品号查询一些信息
	public BuluResult selectXGoods(HttpServletRequest request,Integer goodsId) {
		String sign = request.getParameter("sign");
		Integer dealerId = Integer.parseInt(redis.get(sign));
		Integer type = dealerDao.selectType(dealerId);
		List<Map<String, Object>> scend = new ArrayList<>();
		Map<String, Object> map = dealerDao.selectXGoods(goodsId);
		if(type==0){//一级
			map.put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("firstProfit"));
		}else if(type==1){//二级
			map.put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("secondProfit"));
		}else{//自营二级
			map.put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("ziyingProfit"));
		}
		if((Integer)map.get("type")==0){//布艺帘
			List<Map<String, Object>> list = dealerDao.selectYongliang(goodsId);
			for(int a=0;a<list.size();a++){
				if((Integer)list.get(a).get("type")==0){//类型是布
					if((Integer)map.get("special")==1){//是特价
						if((Integer)map.get("style")==0){//打孔
							list.get(a).put("danjia", map.get("holeSpecPrice"));
							list.get(a).put("zongjia", (Double)map.get("holeSpecPrice")*(Double)list.get(a).get("dosageNumber"));
						}else{//韩折
							list.get(a).put("danjia", map.get("zheSpecPrice"));
							list.get(a).put("zongjia", (Double)map.get("zheSpecPrice")*(Double)list.get(a).get("dosageNumber"));
						}
					}else{//不是特价
						if((Integer)map.get("style")==0){//打孔
							list.get(a).put("danjia", map.get("holePrice"));
							list.get(a).put("zongjia", (Double)map.get("holePrice")*(Double)list.get(a).get("dosageNumber"));
						}else{//韩折
							list.get(a).put("danjia", map.get("zhePrice"));
							list.get(a).put("zongjia", (Double)map.get("zhePrice")*(Double)list.get(a).get("dosageNumber"));
						}
					}
				}else{
					//根据型号查询出来出售价
					Double danjia = dealerDao.selectDanjia((String)list.get(a).get("serialnumber"));
					list.get(a).put("danjia", danjia);
					list.get(a).put("zongjia", danjia*(Double)list.get(a).get("dosageNumber"));
				}
			}
			map.put("zucheng", list);
			List<Map<String, Object>> peijian = dealerDao.selectPeijian(goodsId);
			for(int i=0;i<peijian.size();i++){
				peijian.get(i).put("danjia", peijian.get(i).get("price"));
				peijian.get(i).put("dosageNumber", (Double)peijian.get(i).get("saleNumber")*(Integer)peijian.get(i).get("number"));
				if(type==0){//一级
					peijian.get(i).put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("firstProfit"));
				}else if(type==1){//二级
					peijian.get(i).put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("secondProfit"));
				}else{//自营二级
					peijian.get(i).put("goodsProfit", (Double)map.get("goodsPrice")*(Double)map.get("ziyingProfit"));
				}
			}
			scend.add(map);
			scend.addAll(peijian);
		}else if((Integer)map.get("type")==1){//成品帘
			map.put("danjia", map.get("price"));
			map.put("dosageNumber", (Double)map.get("clothHigh")*(Double)map.get("clothWide"));
			scend.add(map);
		}else{//配件
			map.put("danjia", map.get("price"));
			map.put("dosageNumber", (Double)map.get("saleNumber")*(Integer)map.get("number"));
			scend.add(map);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(scend);
		return buluResult;
	}

	//查询广告
	public BuluResult selectGuanggao() {
		List<String> guanggao = dealerDao.selectGuanggao();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(guanggao);
		return buluResult;
	}
	
}